package com.ruoyi.article.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.article.domain.ArticleCategoryReferenced;
import com.ruoyi.article.domain.ArticleDetail;
import com.ruoyi.article.domain.ArticleTagReferenced;
import com.ruoyi.article.mapper.*;
import com.ruoyi.article.vo.AddTagAndCategoryRequest;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.article.domain.Article;
import com.ruoyi.article.service.IArticleService;
import org.springframework.util.CollectionUtils;

/**
 * 文章 Service业务层处理
 * 
 * @author rey
 * @date 2022-02-04
 */
@Service
public class ArticleServiceImpl implements IArticleService 
{
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleDetailMapper articleDetailMapper;

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ArticleTagReferencedMapper articleTagReferencedMapper;

    @Autowired
    private ArticleCategoryReferencedMapper articleCategoryReferencedMapper;

    /**
     * 查询文章 
     * 
     * @param articleId 文章 主键
     * @return 文章 
     */
    @Override
    public Article selectArticleByArticleId(Long articleId)
    {
        Article article = articleMapper.selectArticleByArticleId(articleId);
        ArticleDetail articleDetail = new ArticleDetail();
        articleDetail.setArticleId(articleId);
        List<ArticleDetail> articleDetails = this.articleDetailMapper.selectArticleDetailList(articleDetail);
        articleDetails.stream().findFirst().ifPresent(fir->{
            article.setContent(fir.getContentMd());
        });
        ArticleTagReferenced articleTagReferenced = new ArticleTagReferenced();
        articleTagReferenced.setArticleId(articleId);
        List<ArticleTagReferenced> articleTagReferenceds = articleTagReferencedMapper.selectArticleTagReferencedList(articleTagReferenced);
        if(!CollectionUtils.isEmpty(articleTagReferenceds)){
            article.setTagIds(articleTagReferenceds.stream().map(ArticleTagReferenced::getTagId).collect(Collectors.toList()));
        }

        ArticleCategoryReferenced articleCategoryReferenced = new ArticleCategoryReferenced();
        articleCategoryReferenced.setArticleId(articleId);
        List<ArticleCategoryReferenced> articleCategoryReferenceds = articleCategoryReferencedMapper.selectArticleCategoryReferencedList(articleCategoryReferenced);
        if(!CollectionUtils.isEmpty(articleCategoryReferenceds)){
            article.setTypeIds(articleCategoryReferenceds.stream().map(ArticleCategoryReferenced::getCategoryId).collect(Collectors.toList()));
        }

        return article;
    }

    /**
     * 查询文章 列表
     * 
     * @param article 文章 
     * @return 文章 
     */
    @Override
    public List<Article> selectArticleList(Article article)
    {
        List<Article> articles = articleMapper.selectArticleList(article);
        articles.forEach(item->{
            ArticleTagReferenced articleTagReferenced = new ArticleTagReferenced();
            articleTagReferenced.setArticleId(item.getArticleId());
            List<ArticleTagReferenced> articleTagReferenceds = articleTagReferencedMapper.selectArticleTagReferencedList(articleTagReferenced);
            if(!CollectionUtils.isEmpty(articleTagReferenceds)){
                item.setTagIds(articleTagReferenceds.stream().map(ArticleTagReferenced::getTagId).collect(Collectors.toList()));
            }

            ArticleCategoryReferenced articleCategoryReferenced = new ArticleCategoryReferenced();
            articleCategoryReferenced.setArticleId(item.getArticleId());
            List<ArticleCategoryReferenced> articleCategoryReferenceds = articleCategoryReferencedMapper.selectArticleCategoryReferencedList(articleCategoryReferenced);
            if(!CollectionUtils.isEmpty(articleCategoryReferenceds)){
                item.setTypeIds(articleCategoryReferenceds.stream().map(ArticleCategoryReferenced::getCategoryId).collect(Collectors.toList()));
            }
        });
        return articles;
    }

    /**
     * 新增文章 
     * 
     * @param article 文章 
     * @return 结果
     */
    @Override
    public int insertArticle(Article article)
    {
        article.setCreateTime(DateUtils.getNowDate());
        article.setCommentCount(0L);
        article.setLikeCount(0L);
        article.setReadCount(0L);
        article.setCreateBy("admin");
        article.setCreateTime(new Date());
        article.setUpdateBy("admin");
        article.setUpdateTime(new Date());
        String s = Optional.ofNullable(article.getContent()).map(String::trim).map(str -> str.substring(0, 50)+"...").orElse(null);
        if("top".equals(article.getTopFlag())){
            article.setPushData(new Date());
        }
        article.setArticleSummary(s);
        articleMapper.insertArticle(article);
        ArticleDetail ad = new ArticleDetail();
        ad.setContentMd(article.getContent());
        ad.setContentHtml(article.getHtmlContent());
        ad.setArticleId(article.getArticleId());
        ad.setCreateBy("admin");
        ad.setCreateTime(new Date());
        ad.setUpdateBy("admin");
        ad.setUpdateTime(new Date());
        ad.setArticleId(article.getArticleId());
        this.articleDetailMapper.insertArticleDetail(ad);
        return 1;
    }

    /**
     * 修改文章 
     * 
     * @param article 文章 
     * @return 结果
     */
    @Override
    public int updateArticle(Article article)
    {
        article.setCreateBy("admin");
        article.setCreateTime(new Date());
        article.setUpdateBy("admin");
        article.setUpdateTime(new Date());
        String s = Optional.ofNullable(article.getContent()).map(String::trim).map(str -> str.substring(0, 50)+"...").orElse(null);
        if("top".equals(article.getTopFlag())){
            article.setPushData(new Date());
        }
        article.setArticleSummary(s);
        articleMapper.updateArticle(article);
        ArticleDetail articleDetail = new ArticleDetail();
        articleDetail.setArticleId(article.getArticleId());
        List<ArticleDetail> articleDetails = this.articleDetailMapper.selectArticleDetailList(articleDetail);
        articleDetails.stream().findFirst().ifPresent(fir->{
            fir.setContentMd(article.getContent());
            fir.setContentHtml(article.getHtmlContent());
            fir.setUpdateBy("admin");
            fir.setUpdateTime(new Date());
            this.articleDetailMapper.updateArticleDetail(fir);
        });

        return 1;
    }

    /**
     * 批量删除文章 
     * 
     * @param articleIds 需要删除的文章 主键
     * @return 结果
     */
    @Override
    public int deleteArticleByArticleIds(Long[] articleIds)
    {
        List<Long> ids = Arrays.asList(articleIds);
        articleDetailMapper.deleteArticleDetailByArticleIds(ids);
        return articleMapper.deleteArticleByArticleIds(articleIds);
    }

    /**
     * 删除文章 信息
     * 
     * @param articleId 文章 主键
     * @return 结果
     */
    @Override
    public int deleteArticleByArticleId(Long articleId)
    {
        articleMapper.deleteArticleByArticleId(articleId);
        articleDetailMapper.deleteArticleDetailByArticleIds(Collections.singletonList(articleId));
        return 1;
    }

    @Override
    public int submitArticleByArticleIds(Long[] articleIds,Integer enable) {

        return articleMapper.submitArticleByArticleIds(articleIds,enable);
    }

    @Override
    public int addTagAndCategory(AddTagAndCategoryRequest request){
        this.articleCategoryReferencedMapper.deleteByArticleId(request.getArticleId());
        this.articleTagReferencedMapper.deleteByArticleId(request.getArticleId());
        if(!CollectionUtils.isEmpty(request.getTagIds())) {
            request.getTagIds().forEach(id->{
                ArticleTagReferenced articleTagReferenced = new ArticleTagReferenced();
                articleTagReferenced.setArticleId(request.getArticleId());
                articleTagReferenced.setTagId(id);
                this.articleTagReferencedMapper.insertArticleTagReferenced(articleTagReferenced);
            });
        }
        if(!CollectionUtils.isEmpty(request.getTypeIds())) {
            request.getTypeIds().forEach(id->{
                ArticleCategoryReferenced articleCategoryReferenced = new ArticleCategoryReferenced();
                articleCategoryReferenced.setArticleId(request.getArticleId());
                articleCategoryReferenced.setCategoryId(id);
                this.articleCategoryReferencedMapper.insertArticleCategoryReferenced(articleCategoryReferenced);
            });

        }
        return 1;
    }
}
